Replace special characters using Power Automate
Table of contents:
Have you ever faced it? A system, that accepts just international characters while users input local ones, like Polish? Or Spanish? Well, Power Automate is here to help them get replaced!
This can be done using just two steps, although the replacement itself may be not very efficient when used to handle really long texts. I am using it to replace short strings, like titles or product names. If you plan to use it for a long text, just observe how much time would it need.
Replacement matrix
First thing is to create a replacement matrix. This can be done to replace any character to something else or to nothing. Do it using a JSON object and a “Compose” action. In my example you can find all Polish special characters:
{ "ą": "a", "Ą": "A", "ę": "e", "Ę": "E", "ć": "c", "Ć": "C", "ł": "l", "Ł": "L", "ó": "o", "Ó": "O", "ś": "s", "Ś": "S", "ź": "z", "Ź": "Z", "ż": "z", "Ż": "Z", "ń": "n", "Ń": "N" }
Replace characters
Next, you need to use the “Select” action, to get a range of every single character, then to iterate through characters, and to lookup their presence in the replacement matrix:
range(0, length({TEXT TO CHANGE}))
“Range” expression returns an array of integers indicating start of each character in the string. And then the map:
if(not(empty(outputs('Replacement_matrix')?[substring({TEXT TO CHANGE}, item(), 1)])), outputs('Replacement_matrix')[substring({TEXT TO CHANGE}, item(), 1)], substring({TEXT TO CHANGE}, item(), 1))
Map part of the action looks up each not empty character from the string, that starts on value from the array of strings and has 1 character length, if is present in the replacement matrix. If yes – it’s being replaced.
Result
What the “Select” action returns is an array of each single character from the input string. Final step then is to simply join the array into a string. Use “Compose” action again and the following expression:
join(body('Replace_characters'), '')
Let’s see it in action
First I passed a short string: “Zażółć gęślą jaźń” to test the solution. It took the whole process around 100ms to run:
I then used chatGPT to generate me a text using the following query: “Generate one paragraph having 20 sentences each having at least 10 words of random text in Polish.“. The result was quite a long text:
Process needed around 200ms to complete. I asked chatGPT to generate me a novel about Warsaw, that has at least 500 words. Needless to say, it’s quite an amount of text 🙂 Still, the time needed to complete the action was really short: about 330ms!
Let me know how you are using this solution and how much time it needs on certain cases.
Dan Robson
This is great and almost exactly what I was looking for including the list of Polish characters that I need to substitute in one of my flows! But I also need to substitute single quote ‘ but adding “‘”:”” to the replacement matrix doesn’t seem to work, my output still has the ‘ in it, would I need to do something special for a single quote?
Tomasz Poszytek
Just be sure this is really just a single quote, not any of the special characters looking similarly (https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html). What I always do, is to use any online URL encoder, to check what is the encoded value.